﻿Imports USA411
Imports HomeSeer2.application
Imports Scheduler.hsapplication
Imports System.Data

''' <summary>
''' HomeSeer2 Interface
''' </summary>
''' <remarks></remarks>
''' 

Public Class HomeSeer2Engine

    Private _hss As New HomeSeer2.application
    Private _hs As Scheduler.hsapplication

    Public Enum DEVICE_COMMAND
        All_Units_Off = 0
        All_Lights_On = 1
        UOn = 2
        UOff = 3
        UDim = 4
        UBright = 5
        All_Lights_Off = 6
        Extended_Code = 7
        Hail_Request = 8
        HAil_Ack = 9
        Preset_Dim1 = 10
        Preset_Dim2 = 11
        Ex_Data_Xfer = 12
        Status_On = 13
        Status_Off = 14
        Status_Request = 15
        Dim_To_Off = 16
        No_Cmd = 17
        Stat_Unknown = 17
        Any_Cmd = 18
        Value_Set = 19
        Value_Increment = 20
        Set_On = 21
        Set_Off = 22
        Set_Any = 23
        Value_Decrement = 24
    End Enum

    Sub New(ByVal Server As String, ByVal userid As String, ByVal password As String)
        _Server = Server
        _UserID = userid
        _Password = password
        _hss.SetHost(_Server)
        Dim res As String = _hss.Connect(_UserID, _Password)
        ' MsgBox(res)
        _hs = _hss.GetHSRef()
    End Sub


    Private _Actions As DataTable
    Public Property Actions() As DataTable
        Get
            Return _Actions
        End Get
        Set(ByVal value As DataTable)
            _Actions = value
        End Set
    End Property

    Private _Server As String
    Public Property Server() As String
        Get
            Return _Server
        End Get
        Set(ByVal value As String)
            _Server = value
        End Set
    End Property

    Private _UserID As String
    Public Property UserID() As String
        Get
            Return _UserID
        End Get
        Set(ByVal value As String)
            _UserID = value
        End Set
    End Property

    Private _Password As String
    Public WriteOnly Property Password() As String
        Set(ByVal value As String)
            _Password = value
        End Set
    End Property

#Region "Public Methods"
    Public Sub ProcessActions(ByVal action As Integer())
        For Each i As Integer In action
            For Each dr As DataRow In Actions.Rows
                If dr.Item("ActionID") = i Then
                    Select Case dr.Item("ActionType")
                        Case 1
                            RunEvent(dr.Item("ActionCmd"))
                        Case 2
                            If dr.Item("ActionCmd") = "Dim" Then
                                ExecuteX10(dr.Item("DeviceCode"), dr.Item("ActionCMD"), 100)
                            Else
                                ExecuteX10(dr.Item("DeviceCode"), "Dim", dr.Item("Level"))
                            End If
                        Case 3
                            '
                        Case 4
                            '
                        Case Else
                            '
                    End Select
                End If
            Next
        Next
    End Sub


    Public Sub SetDeviceStatus(ByVal devCode As String, ByVal Status As Integer)
        _hs.SetDeviceStatus(devCode, Status)
    End Sub
    Public Sub SetDeviceValue(ByVal devCode As String, ByVal Value As Integer)
        _hs.SetDeviceValue(devCode, Value)
    End Sub
    Public Sub ExecuteX10(ByVal devCode As String, ByVal Status As String, ByVal Level As Byte)
        If Status = "Dim" Then
            _hs.ExecX10(devCode, Status, Level)
        Else
            _hs.ExecX10(devCode, Status)
        End If
    End Sub
    Public Sub RunEvent(ByVal EventName As String)
        _hs.TriggerEvent(EventName)
    End Sub

    Public Function GetLog() As String
        Return _hs.LogGet
    End Function

#End Region

End Class
